<template>
    <div class="CarUser-container content-wrapper">
        <div class="filter-container">
            <el-form :model="listQuery" :inline="true" ref="filterForm" style="display: inline-block">
                <el-form-item prop="realname" class="filter-item">
                    <el-input v-model="listQuery.realname" placeholder="姓名" style="width: 200px;"></el-input>
                </el-form-item>
                <el-form-item prop="city" class="filter-item">
                    <el-select v-model="listQuery.city" placeholder="城市" style="width: 150px">
                        <el-option
                                v-for="item in carAreaOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="area" class="filter-item">
                    <el-select v-model="listQuery.area" placeholder="区域" style="width: 150px">
                        <el-option
                                v-for="item in carCityOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="orderBy" class="filter-item">
                    <el-select v-model="listQuery.orderBy" placeholder="排序" style="width: 150px">
                        <el-option
                                v-for="item in sortOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-button class="filter-item" type="primary" icon="search" @click="handleFilter">搜索</el-button>
                <el-button class="filter-item" type="primary" icon="mars-reset" @click="handleResetFilter">重置
                </el-button>

            </el-form>
        </div>

        <el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
                  style="width: 100%">

            <el-table-column label="ID" width="65" align="center">
                <template slot-scope="scope"> {{scope.row.id}} </template>
            </el-table-column>

            <el-table-column label="城市" width="100px" align="center">
                <template slot-scope="scope"> {{scope.row.city}} </template>
            </el-table-column>
            <el-table-column label="区域" width="100px" align="center">
                <template slot-scope="scope"> {{scope.row.area}} </template>
            </el-table-column>
            <el-table-column label="姓名" width="100px" align="center">
                <template slot-scope="scope"> {{scope.row.realname}} </template>
            </el-table-column>
            <el-table-column label="性别" width="100px" align="center">
                <template slot-scope="scope"> {{scope.row.gender | sexFilter}} </template>
            </el-table-column>
            <el-table-column label="最近操作时间" width="" align="left">
                <template slot-scope="scope">
                    {{scope.row.checkTime}} <el-tag type="danger">{{scope.row.opatorTime}}</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="创建时间" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}')}} </template>
            </el-table-column>
            <el-table-column label="更新时间" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.updateTime | parseTime('{y}-{m}-{d} {h}:{i}')}} </template>
            </el-table-column>

            <el-table-column label="操作" width="180px" align="center">
                <template slot-scope="scope">
                    <el-button size="small" type="primary" icon="edit" @click="handleDetail(scope.row)">详情</el-button>
                </template>
            </el-table-column>

        </el-table>

        <div v-show="!listLoading" class="pagination-container">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.page"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="listQuery.rows"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>


    </div>
</template>

<script>
    import { created, deleted, updated, fetchObject, fetchList } from '@/api/tukuan/user'

    const sortOptions = [{label: '按ID升序', key: 'id asc'}, {label: '按ID降序', key: 'id desc'}]
    const carAreaOptions = [
        {label: '广州', key: '广州'},
        {label: '佛山', key: '佛山'},
        {label: '东莞', key: '东莞'}
    ]
    const carCityOptions = [
        {label: '白云区', key: '白云区'},
        {label: '天河区', key: '天河区'},
        {label: 'C', key: 'C'},
        {label: 'D', key: 'D'},
        {label: 'E', key: 'E'},
        {label: 'F', key: 'F'},
        {label: 'G', key: 'G'},
        {label: 'H', key: 'H'},
        {label: 'I', key: 'I'},
        {label: 'J', key: 'J'},
        {label: 'K', key: 'K'},
        {label: 'L', key: 'L'},
        {label: 'M', key: 'M'},
        {label: 'N', key: 'N'}
    ]

    export default {
        name: 'CarUser',
        data() {
            return {
                list: null,
                total: null,
                listLoading: true,
                listQuery: {
                    page: 1,
                    rows: 10,
                    realname: undefined,
                    city: undefined,
                    area: undefined,
                    orderBy: 'check_time desc'
                },
                sortOptions,
                carAreaOptions,
                carCityOptions,

            }
        },
        created() {
            this.getList()
        },
        filters: {
            sexFilter(sex) {
                const sexArray = ['男', '女', '未知']
                return sexArray[sex?--sex:'']
            },
            statusFilter(status) {
                const statusArray = ['正常', '锁定']
                return statusArray[status?--status:'']
            },
            statusTagFilter(status) {
                const statusArray = ['success', 'danger']
                return statusArray[status?--status:'']
            }
        },
        methods: {
            getList() {
                this.listLoading = true
                fetchList(this.listQuery).then(response => {
                    this.list = response.data.list
                    this.total = response.data.total
                    this.listLoading = false
                }).catch((error)=>{
                    console.log(error)
                    this.listLoading = false
                })
            },
            handleSizeChange(val) {
                this.listQuery.rows = val
                this.getList()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getList()
            },
            handleCloseDialog() {
                this.resetForm()
            },
            handleFilter() {
                this.listQuery.page = 1
                this.getList()
            },
            handleResetFilter() {
                this.$refs.filterForm.resetFields()
                this.getList()
            },
            handleDetail(row) {
                this.$router.push({'path': `/car/dq-check-detail/${row.id}/${row.realname}`})
            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>

</style>